﻿
package extremefx.geom {
	import flash.geom.Point;	
	
	import extremefx.IEnumerable;
	import extremefx.IEnumerator;	

	/**
	 * @author Marcelo Volmaro
	 */
	public class Grid implements IEnumerable, IEnumerator{

		public var width:Number;
		public var height:Number;
		
		private var _rows:uint;
		private var _cols:uint;
		private var _r:uint;
		private var _c:int;

		public function Grid(pCols:uint, pRows:uint, pWidth:Number = 1, pHeight:Number = 1) {
			_rows = pRows;
			_cols = pCols;
			width = pWidth;
			height = pHeight;
		}
		
		public function getIndexFor(pCol:uint, pRow:uint):uint {
			return Math.floor(pRow / height) * _cols + Math.floor(pCol / width);
		}
		
		public function getPositionFor(pIndex:uint):Point {
			return new Point(Math.floor(pIndex / _cols), pIndex % _cols);
		}
		
		public function getEnumerator():IEnumerator {
			reset();
			return this;
//			return new Grid(_rows, _cols, _w, _h);
		}
		
		public function moveNext():Boolean {
			++_c;
			
			if (_c == _cols) {
				_c = 0;
				++_r;
			}
			
			return _r != _rows;
		}
		
		public function reset():void {
			_r = 0;
			_c = -1;
		}
		
		public function get current():* {
			return new Point(_c * width, _r * height);
		}
	}
}
